package com.example.wj.service;

import com.example.wj.dao.BookDao;
import com.example.wj.pojo.Book;
import com.example.wj.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
@Autowired
    BookDao bookDao;
@Autowired
    CategoryService categoryService;

  public    List<Book> list(){
        Sort sort=Sort.by(Sort.Direction.DESC,"id");
        List<Book> all = bookDao.findAll(sort);
        return all;
    }
    public  int  save(Book book){
        Book save = bookDao.save(book);
        if (save!=null){
            return 1;
        }else{
            return 0;
        }

    }
    public void delete(int id){
    bookDao.deleteById(id);
    }
    public List<Book> listbycategroy(int id){
           Category category=categoryService.get(id);
        List<Book> byCategory = bookDao.findByCategory(category);
        return byCategory;

    }
    public List<Book> findByAuthor(String author){
        List<Book> byAuthor = bookDao.findByAuthor(author);
        return byAuthor;
    }
   public Book findByTitle(String title){
       Book byTitle = bookDao.findByTitle(title);
       return byTitle;
   }
   public Book findById(int id){
       Book byId = bookDao.findById(id);
       return byId;
   }

}
